#include <iostream>
#include <vector>
#include <map>


using namespace std;
const int N = 1e3;
int q[10];
int arr[N];

int main() {
	int n, k;
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; ++ i) {
		int num;
		scanf("%d", &num);
		arr[num] ++;
	}
	for (int i = 1; i <= k; ++ i) {
		int a, b;
		scanf("%d %d", &a, &b);
		for (int j = 0; j <= 9; ++ j) if (j != a && j != b) q[j] ++;
	}
	int max_[5] = {0};
	for (int i = 0; i < N; ++ i) {
		if (arr[i]) {
			int a = arr[i] % 10, b = arr[i] / 10 % 10, c = arr[i] / 100, res = 0;
			for (int j = 0; j <= 9; ++ j) {
				if (j != a && j != b && j != c) res += q[j];
			}
			arr[i] = res *= arr[i];
			for (int j = 0; j < 3; ++ j) {
				if (res >= arr[max_[j]]) {
					max_[j + 2] = max_[j + 1], max_[j + 1] = max_[j], max_[j] = i;
					break;
				}
			}
		}
	}
	printf("%03d %03d %03d", max_[0], max_[1], max_[2]);
	return 0;
}